package ua.soso.dao.hibernate;

import org.hibernate.Query;
import ua.soso.dao.UserDao;
import ua.soso.dao.model.User;


/**
 * Created by Babka Aleksey
 * Date: 06.04.12
 */
public class HibernateUserDao extends HibernateBaseDao<User> implements UserDao {
    public HibernateUserDao() {
        super(User.class);
    }

    @Override
    public User findByLoginAndPass(String login, String pass) {
        Query query = session().createQuery("from User u where" +
                " u.login = :LOGIN " +
                " and u.password = :PASSWORD");

        query.setParameter("LOGIN", login);
        query.setParameter("PASSWORD", pass);

        return (User) query.uniqueResult();
    }

    @Override
    public void saveUser(User user) {
        session().save(user);
    }

    @Override
    public User findByLogin(String login) {
        Query query = session().createQuery("from User u where" +
                " u.login = :LOGIN ");

        query.setParameter("LOGIN", login);

        return (User) query.uniqueResult();
    }
}
